package com.foreknow.elm.service.impl;

import com.foreknow.elm.dao.CartDao;
import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.dao.impl.CartDaoImpl;
import com.foreknow.elm.dao.impl.OrderdetailetDaoImpl;
import com.foreknow.elm.dao.impl.OrdersDaoImpl;
import com.foreknow.elm.po.Cart;
import com.foreknow.elm.po.Orderdetailet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.service.OrdersService;
import com.foreknow.elm.util.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    public int screateOrders(Orders orders) {
        List<Orderdetailet> list = new ArrayList<>();
        int i = 0;

        CartDao cartDao = new CartDaoImpl();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();

        try {
            DBUtil.beginTransaction();
            List<Cart> cartList = cartDao.dlistCart(orders.getUserId(),orders.getBusinessId());
            i = ordersDao.dcreateOrders(orders);
            for (int a = 0;a < cartList.size();a++){
                Orderdetailet orderdetailet = new Orderdetailet();
                orderdetailet.setOrderId(i);
                orderdetailet.setFoodId(cartList.get(a).getFoodId());
                orderdetailet.setQuantity(cartList.get(a).getQuantity());
                list.add(orderdetailet);
            }

            orderdetailetDao.daoinsetorderdetailet(list);
            for (int j = 0; j < cartList.size(); j++) {
                cartDao.dremoveCart(cartList.get(j));
            }
            DBUtil.commitTransaction();


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
            try {
                DBUtil.rollbackTransaction();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return i;
    }

    @Override
    public Orders sgetOrdersById(Integer orderid) {
        Orders orders = new Orders();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            orders = ordersDao.dgetOrdersById(orderid);
            List<Orderdetailet> list= orderdetailetDao.daoselectorderdetailet(orderid);
            orders.setOrderdetailet(list);

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return orders ;
    }

    @Override
    public List<Orders> slistOrdersByUserId(String userid) {
        List<Orders> list = new ArrayList<>();
        OrdersDao ordersDao = new OrdersDaoImpl();
        OrderdetailetDao orderdetailetDao = new OrderdetailetDaoImpl();
        try {
            list = ordersDao.dlistOrdersByUserId(userid);
            for (int i = 0;i<list.size();i++){
                List<Orderdetailet> orderdetailetList = orderdetailetDao.daoselectorderdetailet(list.get(i).getOrderId());
                list.get(i).setOrderdetailet(orderdetailetList);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
